package com.itfreer.gis.tansform;

import com.itfreer.file.LocalObjectBase;
import com.itfreer.gis.data.file.ShapeFileDataEditServer;
import com.itfreer.gis.map.Application;
import com.itfreer.utils.geotools.CoordinateTransformUtils;
import com.itfreer.utils.zip.ZipperUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opengis.feature.simple.SimpleFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class Wgs84ToGcj02 extends LocalObjectBase {

	@Autowired
	private ShapeFileDataEditServer shapeFileAnalysis;

	// 原文件相对路径
	private String sourcePath = "export/20200620/asdd/lg/lg.zip";

	// 84坐标系shp转火星shp
	@Test
	public void test() throws Exception {

		// 根据原文件路径获取shp文件的数据
		List<SimpleFeature> simpleFeatures = getFeature(sourcePath, null);

		// 定义转换后的数据集合
		List<SimpleFeature> newF = new ArrayList<>();

		newF = CoordinateTransformUtils.transformSimpleFeature(simpleFeatures, 5);

		export(newF, "ZY_LDGX_2018_gcj02");

	}

	public void export(List<SimpleFeature> datas, String target) throws Exception {
		// 将导出数据，放到系统运行的临时目录
		String basePath = getBasePath();
		if (!basePath.endsWith("/")) {
			basePath += "/";
		}
		File basePathFile = new File(basePath);
		if (!basePathFile.isDirectory()) {
			basePathFile.mkdirs();
		}

		// 临时路径
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String ymd = sdf.format(new Date());
		String filePath = "export/" + ymd + "/" + UUID.randomUUID().toString().replaceAll("-", "") + "/";
		File filePathFile = new File(basePath + filePath);
		if (!filePathFile.isDirectory()) {
			filePathFile.mkdirs();
		}

		if (datas == null) {
			datas = new ArrayList<>();
		}
		// 将数据导出到临时文件中
		String outFilePath = basePath + filePath + target + ".shp";
		shapeFileAnalysis.addFeatures(outFilePath, datas, null);

		// 压缩文件
		String zipFilePath = basePath + filePath + target + ".zip";
		FileOutputStream zipFile = new FileOutputStream(zipFilePath);
		List<ZipperUtils.FileEntry> fileEntrys = new ArrayList<>();

		String dbfFilePath = basePath + filePath + target + ".dbf";
		addZip(fileEntrys, dbfFilePath);

		String prjFilePath = basePath + filePath + target + ".prj";
		addZip(fileEntrys, prjFilePath);

		String shpFilePath = basePath + filePath + target + ".shp";
		addZip(fileEntrys, shpFilePath);

		String shxFilePath = basePath + filePath + target + ".shx";
		addZip(fileEntrys, shxFilePath);

		String fixFilePath = basePath + filePath + target + ".fix";
		addZip(fileEntrys, fixFilePath);

		ZipperUtils.zip(zipFile, fileEntrys);
	}

	public List<SimpleFeature> getFeature(String source, String where) throws Exception {
		// 解压文件
		String basePath = getBasePath();
		if (!basePath.endsWith("/")) {
			basePath += "/";
		}
		source = basePath + source;

		String unZipFile = source.replace(".ZIP", "").replace(".zip", "");
		File dir = new File(unZipFile);
		if (!dir.isDirectory()) {
			dir.mkdirs();
		}
		ZipperUtils.upZipFile(new File(source), unZipFile);
		FilenameFilter filter = new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.endsWith(".shp") || name.endsWith(".SHP");
			}
		};
		String[] children = dir.list(filter);
		if (children == null || children.length <= 0) {
			return null;
		}

		String shapeFile = unZipFile + "/" + children[0];

		List<SimpleFeature> datas = shapeFileAnalysis.queryFeature(shapeFile, where);

		return datas;
	}

	private void addZip(List<ZipperUtils.FileEntry> fileEntrys, String fixFilePath) {
		File fixFile = new File(fixFilePath);
		if (fixFile.exists()) {
			ZipperUtils.FileEntry fixFileEntry = new ZipperUtils.FileEntry(null, fixFile);
			fileEntrys.add(fixFileEntry);
		}
	}

}
